import json
import sys

from framework.common.pt_logger import logger
from framework.core.crawler_task import Task
from util import queueWrapper

overridden_parameters = {
    "enableNotification": False
}

spider_module_name_map = {  # JimmyMo: 作为Key的CoreProductsBestSellers是从Initiator中的metadata中的tt中带入的
    "CoreProductsBestSellers": {"ModuleName": "spiders.coreProducts_spider.coreProducts_spider", "Parameters": {"spider_name": "CoreProductsBestSellers", "s3_bucket": "v2-data-production"}},
    "CoreProductsMostWishedFor": {"ModuleName": "spiders.coreProducts_spider.coreProducts_spider", "Parameters": {"spider_name": "CoreProductsMostWishedFor", "s3_bucket": "v2-data-production"}},
    "CoreProductsGiftIdeas": {"ModuleName": "spiders.coreProducts_spider.coreProducts_spider", "Parameters": {"spider_name": "CoreProductsGiftIdeas", "s3_bucket": "v2-data-production"}},
    "CoreProductsMoversAndShakers": {"ModuleName": "spiders.coreProducts_spider.coreProducts_spider", "Parameters": {"spider_name": "CoreProductsMoversAndShakers", "s3_bucket": "v2-data-production"}},
    "CoreProductsNewReleases": {"ModuleName": "spiders.coreProducts_spider.coreProducts_spider", "Parameters": {"spider_name": "CoreProductsNewReleases", "s3_bucket": "v2-data-production"}},
    "AsinDetailPage": {"ModuleName": "spiders.asin_spider.asin_spider", "Parameters": {"spider_name": "AsinDetailPage", "s3_bucket": "v2-data-production"}},
    "AsinCustomerReview": {"ModuleName": "spiders.asin_spider.asin_spider", "Parameters": {"spider_name": "AsinCustomerReview", "s3_bucket": "v2-data-production"}},
    "AsinQuestionAndAnswer": {"ModuleName": "spiders.asin_spider.asin_spider", "Parameters": {"spider_name": "AsinQuestionAndAnswer", "s3_bucket": "v2-data-production"}},
}

# SpiderWorker 从sqs中接受一个Task，并开始工作，产生一系列Result或新的Task
def processRequest(messageList, event=None, context=None):
    """
    messageList format reference: (from queueWrapper, sqs)
    [
        {
            "receiptHandle": "AQEBEjm6COIRoFKM1RYbnA9WMLhM6FVlj+9GfvPM521OAFzfqfS+aZkhssqvObRJHvGnXNAGOzkOMCBJjkuDVmyaBRhvNSX5bnhO7UwalNkbC4E9D9pzzKQxmMVG+Af8Jyne7AUem+D0NgBEdoxcm6ShfA8Cu81739fa+yOCdIK0PZerKZ00xNxrMKI0eMI6WvRysGkO/pxa4lt+z3KmaHms8oS55TglG2gPSO8qnlCvz58qLsM1JbvNckJ4FnlnZ5aalpre/2klRagLgr+Tiw8IuepiZAHhfwXusRLRIF9LbA7NGGMw/mH2hdihEgBvXKRxa7FseuzT0ajAQmtrBg2Wt1FB/35Uu9MDFJnkZI8BCN/fwbVI/Wn5NFZkuNihx0aVTR0igA+oJ7RmMGfZsDkNxAKztrDpGP4JabjdkGdZpd88pGzDVA1ZwhoQlPCYnoC9",
            "messageBodyText": "{\n    'url': 'https://www.amazon.com/Best-Sellers-Home-Improvement/zgbs/hi/ref=zg_bs_nav_0/143-0236919-9936602',\n    'metadata': {\n        'batchId': '2021-05-03',\n        'tt': 'CoreProductsBestSellers',\n        'td': 'department',\n        'tp': 'first_page'\n    },\n    'parser': 'default',\n    'desc': null,\n    'tags': {\n        'Name': 'Tools & Home Improvement'\n    },\n    'parent_result': {\n        'source_url': 'https://www.amazon.com/gp/bestsellers',\n        'metadata': {\n            'batchId': '2021-05-03',\n            'tt': 'CoreProductsBestSellers',\n            'td': 'init',\n            'tp': 'first_page',\n            'tr': 'browseNode_tree'\n        },\n        'items': {\n            'departmentName': [\n                ' Any Department'\n            ],\n            'parent': null,\n            'nodeId': 'init'\n        },\n        'tags': {\n            'node_path': [\n                'init'\n            ]\n        }\n    }\n}",
            "metadata": {
                "eventSource": "aws:sqs",
                "eventSourceARN": "arn:aws:sqs:us-east-2:599885534814:pt-spider-serverless-CoreProductsBestSellers-queue",
                "queueName": "pt-spider-serverless-CoreProductsBestSellers-queue",
                "senderId": "AROAYXK7W2ZPCXFNLFWRZ:pt-ss-bs-BSWF-7VHI97OQ0YQA",
                "approximateFirstReceiveTimestamp": "1620005962486",
                "approximateReceiveCount": "3",
                "sentTimestamp": "1620005962485"
            }
        }
    ]
    messageBodyText format reference:
    {
        "url": "https://www.amazon.com/Best-Sellers-Home-Improvement/zgbs/hi/ref=zg_bs_nav_0/143-0236919-9936602",
        "metadata": {
            "batchId": "2021-05-03",
            "tt": "CoreProductsBestSellers",
            "td": "department",
            "tp": "first_page"
        },
        "parser": "default",
        "desc": null,
        "tags": {
            "Name": "Tools & Home Improvement"
        },
        "parent_result": {
            "source_url": "https://www.amazon.com/gp/bestsellers",
            "metadata": {
                "batchId": "2021-05-03",
                "tt": "CoreProductsBestSellers",
                "td": "init",
                "tp": "first_page",
                "tr": "browseNode_tree"
            },
            "items": {
                "departmentName": [
                    " Any Department"
                ],
                "parent": null,
                "nodeId": "init"
            },
            "tags": {
                "node_path": [
                    "init"
                ]
            }
        }
    }
    event format reference: (from sqs push)
    {
        "Records": [
                {
                    "messageId": "773348fd-b94e-416b-a5b5-32c48455eb2c",
                    "receiptHandle": "AQEBle+cs6SlNxVXUsU710a10sLCvq4Dv6x4NrIW8hTmMFVyS7rlOfH6W8V0/1I6ABk/dWigB2TM1dn3rh1BvIF5ri9FBxo3IpvvvYn6eRz/+K5V7z17UGWtLmdlwFKSlkjTLYz0X/azPQCWnhuDTQlsraf6pgOqAJTGt6pT+5F4AP4VXNjtEZKhqIgtR5+t0wXtK/oXnxnfvnSz2H8Vjz4nhACTV7ClddM/cXaF0bhZdwWF4A3SKBqpmCctEviMfRvKIMoCTUPw1+/5DQMIoSXJJk6N8PQDAtfziZAAFg6/XiAUnIr+1d3sZAYwtMDu36W2tazew0mud4Q5oONxeqMbxVBHP3ae9jsLI3wWTpLEBXmkqUgglfzj6auyxCog1p6Jt9TxROVTjFS58zAqI3q4l0pBtCp9tQNth20GGW5vpnCFSLyi0eD7dwXMgehVaDBW",
                    "body": "{\n    'url': 'https://www.amazon.com/Best-Sellers-Toys-Games-Students-Round-Edge-Scissors/zgbs/toys-and-games/1084736/ref=zg_bs_nav_t_2_166057011/143-0236919-9936602',\n    'metadata': {\n        'batchId': '2021-05-03',\n        'tt': 'CoreProductsBestSellers',\n        'td': 'node',\n        'tp': 'first_page'\n    },\n    'parser': 'default',\n    'desc': null,\n    'tags': {\n        'Name': 'Scissors'\n    },\n    'parent_result': {\n        'source_url': 'https://www.amazon.com/Best-Sellers-Toys-Games-Arts-Crafts-Supplies/zgbs/toys-and-games/166057011/ref=zg_bs_nav_t_1_t/143-0236919-9936602',\n        'metadata': {\n            'batchId': '2021-05-03',\n            'tt': 'CoreProductsBestSellers',\n            'td': 'rootNode',\n            'tp': 'first_page',\n            'tr': 'browseNode_tree'\n        },\n        'items': {\n            'departmentName': [\n                ' Arts & Crafts'\n            ],\n            'parent': {\n                'departmentName': [\n                    ' Toys & Games'\n                ],\n                'parent': {\n                    'departmentName': [\n                        ' Any Department'\n                    ],\n                    'parent': null,\n                    'nodeId': 'init'\n                },\n                'nodeId': 'toys-and-games'\n            },\n            'nodeId': '166057011'\n        },\n        'tags': {\n            'Name': 'Arts & Crafts',\n            'node_path': [\n                'init',\n                'toys-and-games',\n                '166057011'\n            ]\n        }\n    }\n}",
                    "attributes": {
                        "ApproximateReceiveCount": "1",
                        "SentTimestamp": "1620006083702",
                        "SenderId": "AROAYXK7W2ZPCXFNLFWRZ:pt-ss-bs-BSWF-7VHI97OQ0YQA",
                        "ApproximateFirstReceiveTimestamp": "1620006234336"
                    },
                    "messageAttributes": {},
                    "md5OfMessageAttributes": "None",
                    "md5OfBody": "e850358b5bda57b613047600f35b07da",
                    "eventSource": "aws:sqs",
                    "eventSourceARN": "arn:aws:sqs:us-east-2:599885534814:pt-spider-serverless-CoreProductsBestSellers-queue",
                    "awsRegion": "us-east-2"
                }
            ]
    }
    """
    logger.info("started")
    logger.info("messageList: %s" % messageList)

    sys.path.append('jobs/amazon')
    for message in messageList:
        messageBody = json.loads(message.get("messageBodyText"))
        taskType = messageBody["metadata"]["tt"]
        batchId = messageBody["metadata"]["batchId"]
        spider_module_name = spider_module_name_map[taskType]["ModuleName"]
        __import__(spider_module_name)
        spider_lib = sys.modules[spider_module_name]
        # batch_id = message["metadata"]["queueName"].split("-")[-1]
        spider_lib.batch_id = batchId
        logger.info("batch id is %s" % batchId)

        parameters = list(spider_module_name_map[taskType]["Parameters"].items())
        for item in parameters:
            spider_lib.__dict__[item[0]] = item[1]
            logger.info("Parameter pair key: %s, value: %s" % (item[0], item[1]))
        messageBody = json.loads(message.get("messageBodyText"))
        task = Task.from_json(messageBody)
        spider_lib.start_worker(task)


globals()['queueWrapper'].global_parameters = {**globals()['queueWrapper'].global_parameters, **overridden_parameters}
globals()['queueWrapper'].processRequest = processRequest
globals()['handler'] = queueWrapper.handler